<template>
  <el-dialog v-model="show" title="导入表格" width="500">

    <div style="padding-bottom: 30px; text-align: center">
      <el-link @click="handleTemplate">下载模板</el-link>
    </div>

    <el-form :model="form" class="demo-form-inline">
      <el-form-item label="文件">
        <yu-upload  @update:modelValue="uploadOk" url="/order/order/importExcel" />
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script setup>
import { download } from "@/utils/http.js";
const emit = defineEmits(['refreshDataList']);
const show = ref(false);
const form = ref({});
const init = () => {
  show.value = true;
};

const handleTemplate = () => {
  http({
    url: http.adornUrl("/order/order/deliveryModel"),
    method: "get",
    responseType: "blob", // 解决文件下载乱码问题
  }).then(({ data }) => {
    const blob = new Blob([data], {
      type:
        "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8",
    });
    const fileName = "快递单号导入模板_" + new Date().getTime() + ".xlsx";
    const elink = document.createElement("a");
    if ("download" in elink) {
      // 非IE下载
      elink.download = fileName;
      elink.style.display = "none";
      elink.href = URL.createObjectURL(blob);
      document.body.appendChild(elink);
      elink.click();
      URL.revokeObjectURL(elink.href); // 释放URL 对象
      document.body.removeChild(elink);
    } else {
      // IE10+下载
      navigator.msSaveBlob(blob, fileName);
    }
  });
};






// const handleTemplate = () => {
//   let fileName = "快递单号导入模板_" + new Date().getTime() + ".xlsx";
//   download("order/order/deliveryModel", undefined, fileName);
// };
const uploadOk = (res) => {
  console.log(res);
  if (res.success) {
    ElMessage({
      message: "导入成功！",
      type: "success",
    });
    emit('refreshDataList')
  } else {
    ElMessage({
      message: "导入失败！" + res.msg,
      type: "Error",
    });
  }
  show.value = false;
};

defineExpose({ init });
</script>
